package com.paas.form.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bpm.feign.model.BpmDeCategoryEntity;
import com.bpm.feign.service.IBpmDeCategoryService;
import com.paas.common.util.BeanUtil;
import com.paas.common.util.ListUtil;
import com.paas.common.util.StringUtil;
import com.paas.form.entity.FormViewPubEntity;
import com.paas.form.entity.FormViewResourceEntity;
import com.paas.form.service.IFormAttributePubService;
import com.paas.form.service.IFormViewPubService;
import com.paas.form.service.IFormViewResourceService;
import com.paas.form.vo.FormViewVo;
import com.paas.form.vo.ViewPublishVo;
import com.publics.entity.UcResourcesEntity;
import com.publics.feign.service.IUcResourcesService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.paas.form.dao.FormDefinitionDao;
import com.paas.form.entity.FormDefinitionEntity;
import com.paas.form.service.IFormDefinitionService;
import com.paas.common.web.service.BaseServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("formViewPublishService")
public class FormDefinitionServiceImpl extends BaseServiceImpl<FormDefinitionDao, FormDefinitionEntity> implements IFormDefinitionService {

    //定版视图
    @Autowired
    public IFormViewPubService formViewPubService;

    @Autowired
    public IFormViewResourceService formViewResourceService;

    //流程分类查询
    @Autowired
    public IBpmDeCategoryService bpmDeCategoryService;

    @Autowired
    public IUcResourcesService ucResourcesService;

    @Override
    public FormViewVo getFormDetailById(String formId){
        //定义返回对象
        FormViewVo formViewVo = new FormViewVo();
        //查询表单数据
        FormDefinitionEntity formDefinitionEntity = this.getById(formId);
        if(BeanUtil.isEmpty(formDefinitionEntity) || StringUtil.isEmpty(formDefinitionEntity.getViewId())){
            return null;
        }
        /**
         * 处理流程分类名称
         */
        List<String> processIds = new ArrayList<>();
        if(StringUtil.isNotEmpty(formDefinitionEntity.getApplyProcess())){
            ListUtil.addUniqueValue(processIds,formDefinitionEntity.getApplyProcess());
        }
        if(StringUtil.isNotEmpty(formDefinitionEntity.getChangeProcess())){
            ListUtil.addUniqueValue(processIds,formDefinitionEntity.getChangeProcess());
        }
        if(StringUtil.isNotEmpty(formDefinitionEntity.getDeleteProcess())){
            ListUtil.addUniqueValue(processIds,formDefinitionEntity.getDeleteProcess());
        }
        if(StringUtil.isNotEmpty(formDefinitionEntity.getEnableProcess())){
            ListUtil.addUniqueValue(processIds,formDefinitionEntity.getEnableProcess());
        }
        if(StringUtil.isNotEmpty(formDefinitionEntity.getStopProcess())){
            ListUtil.addUniqueValue(processIds,formDefinitionEntity.getStopProcess());
        }
        //定义key为流程分类id,value为流程分类对象
        Map<String,BpmDeCategoryEntity> bpmDeCategoryMap = new HashMap<String,BpmDeCategoryEntity>();
        if(BeanUtil.isNotEmpty(processIds)){
            List<BpmDeCategoryEntity> bpmDeCategoryList = bpmDeCategoryService.queryBatchByIds(processIds);
            if(bpmDeCategoryList!=null){
                bpmDeCategoryMap = bpmDeCategoryList.stream().collect(Collectors.toMap(BpmDeCategoryEntity::getId, item ->item));
            }
        }
        if(bpmDeCategoryMap!=null){
            //设置开启流程分类名称
            if(bpmDeCategoryMap.containsKey(formDefinitionEntity.getApplyProcess())){
                formDefinitionEntity.setApplyProcessName(bpmDeCategoryMap.get(formDefinitionEntity.getApplyProcess()).getCatgName());
            }
            if(bpmDeCategoryMap.containsKey(formDefinitionEntity.getChangeProcess())){
                formDefinitionEntity.setChangeProcessName(bpmDeCategoryMap.get(formDefinitionEntity.getChangeProcess()).getCatgName());
            }

            if(bpmDeCategoryMap.containsKey(formDefinitionEntity.getDeleteProcess())){
                formDefinitionEntity.setDeleteProcessName(bpmDeCategoryMap.get(formDefinitionEntity.getDeleteProcess()).getCatgName());
            }

            if(bpmDeCategoryMap.containsKey(formDefinitionEntity.getStopProcess())){
                formDefinitionEntity.setStopProcessName(bpmDeCategoryMap.get(formDefinitionEntity.getStopProcess()).getCatgName());
            }

            if(bpmDeCategoryMap.containsKey(formDefinitionEntity.getEnableProcess())){
                formDefinitionEntity.setEnableProcessName(bpmDeCategoryMap.get(formDefinitionEntity.getEnableProcess()).getCatgName());
            }
        }
        //设置表单基本信息
        formViewVo.setFormDefinitionEntity(formDefinitionEntity);
        //查询定版视图和属性
        ViewPublishVo viewPublishVo = formViewPubService.getViewFinalizeById(formDefinitionEntity.getViewId());
        formViewVo.setViewPublishVo(viewPublishVo);
        /**
         * 查询并反填发布菜单信息
         */
        FormViewResourceEntity formViewResourceEntity = formViewResourceService.queryByViewFormId(formDefinitionEntity.getId());
        if(BeanUtil.isNotEmpty(formViewResourceEntity)){
            UcResourcesEntity ucResourcesEntity = ucResourcesService.queryById(formViewResourceEntity.getParentId());
            if (BeanUtil.isNotEmpty(ucResourcesEntity)) {
                formViewResourceEntity.setManuName(ucResourcesEntity.getShowName());
            }
            formViewVo.setFormViewResourceEntity(formViewResourceEntity);
        }

        return formViewVo;
    };

    @Override
    public IPage<FormDefinitionEntity> getFormViewPage(FormDefinitionEntity formDefinitionEntity,int currentPage, int pageSize){
        IPage<FormDefinitionEntity> page = new Page<FormDefinitionEntity>(currentPage,pageSize);
        return this.baseMapper.queryFormViewList(page,formDefinitionEntity);
    }

    /**
     * 更新数据状态
     * @param formId
     * @param publishApprovalStatus
     */
    @Override
    public void updateFormStatus(String formId,String publishApprovalStatus){
        //修改表单表主数据状态
        FormDefinitionEntity updateForm = new FormDefinitionEntity();
        updateForm.setId(formId);
        updateForm.setPublishApprovalStatus(publishApprovalStatus);
        this.updateById(updateForm);
    }

    @Override
    public boolean isExistsForm(String viewId,List<String> bizStatus){
        QueryWrapper<FormDefinitionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("view_id",viewId);
        queryWrapper.in("publish_approval_status",bizStatus);
        int count = this.baseMapper.selectCount(queryWrapper);
        return count>0;
    }
	
}